package com.peakmain.wanandroid.netmodule.net.http.params;

import java.io.File;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

import okhttp3.MediaType;

/**
 * @FileName RequestParams
 * @Description http 请求参数类
 * @Author zhouyong
 * @Date 2016-06-13 10:54
 * @Version V 1.0
 */
public class RequestParams {
    private static final String TAG = "RequestParams";
    /***
     * 普通的键值对参数
     */
    public ConcurrentHashMap<String, String> urlParamsMap = new ConcurrentHashMap<>();
    /***
     * 文的键值对参数
     */
    public ConcurrentHashMap<String, ArrayList<FileWrapper>> fileParamsMap = new ConcurrentHashMap<>();

    private ArrayList<FileWrapper> fileWrapperArr;

    public RequestParams() {

    }

    public void put(String key, String value) {
        if (key != null && value != null) {
            urlParamsMap.put(key, value);
        }
    }

    public void put(String key, int value) {
        if (key != null && value != -1) {
            urlParamsMap.put(key, value + "");
        }
    }


    public void put(String key, File file) {
        put(key, file, file.getName());
    }

    /**
     * 上传多个文件
     */
    public void put(String key, ArrayList<File> fileList) {
        if (fileList.size() != 0) {
            for (File file : fileList) {
                if (file == null || !file.exists() || file.length() == 0) {
                    continue;
                }
                put(key, file, file.getName());
            }
        }
    }


    public void put(String key, File file, String fileName) {
        put(key, file, fileName, guessMimeType(fileName));
    }


    public void put(String key, File file, String fileName, MediaType contentType) {
        if (key != null) {
            fileParamsMap.put(key, putArrFileWrapper(new FileWrapper(file, fileName, contentType)));
        }
    }


    private ArrayList<FileWrapper> putArrFileWrapper(FileWrapper fileWrapper) {
        if (fileWrapperArr == null) {
            fileWrapperArr = new ArrayList<>();
        }
        fileWrapperArr.add(fileWrapper);
        return fileWrapperArr;
    }


    public void put(RequestParams params) {
        if (params != null) {
            if (params.urlParamsMap != null && !params.urlParamsMap.isEmpty()) {
                urlParamsMap.putAll(params.urlParamsMap);
            }
            if (params.fileParamsMap != null && !params.fileParamsMap.isEmpty()) {
                fileParamsMap.putAll(params.fileParamsMap);
            }
        }
    }

    public void removeKey(String key) {
        urlParamsMap.remove(key);
    }

    public void removeFileKey(String key) {
        fileParamsMap.remove(key);
    }

    public void clear() {
        urlParamsMap.clear();
        fileParamsMap.clear();
    }

    private MediaType guessMimeType(String path) {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String contentType = fileNameMap.getContentTypeFor(path);
        if (contentType == null) {
            contentType = "application/octet-stream";
        }
        return MediaType.parse(contentType);
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        for (ConcurrentHashMap.Entry<String, String> entry : urlParamsMap.entrySet()) {
            if (result.length() > 0) result.append("&");
            result.append(entry.getKey()).append("=").append(entry.getValue());
        }
        for (ConcurrentHashMap.Entry<String, ArrayList<FileWrapper>> entry : fileParamsMap.entrySet()) {
            if (result.length() > 0) result.append("&");
            result.append(entry.getKey()).append("=").append(entry.getValue());
        }
        return result.toString();
    }

}
